package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.po.Register;
import com.example.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/register")
public class RegisterController {

    @Autowired
    RegisterService registerService;

    @RequestMapping("/add")
    public Boolean add(@RequestBody Register register){
        register.setRegisttime(new Date());
        register.setVisitstate(1);
        System.out.println(register);
        return registerService.save(register);
    }

    @RequestMapping("/conditionselect")
    public List<Register> conditionselect(@RequestParam Integer userid,
                                          @RequestParam Integer visitstate,
                                           @RequestParam Integer deptid,
                                           @RequestParam String realname){
        LambdaQueryWrapper<Register> queryWrapper = new LambdaQueryWrapper<>( );
        if(userid != 0){
            queryWrapper.eq(Register::getUserid,userid);
        }
        if(visitstate != 0){
            queryWrapper.eq(Register::getVisitstate,visitstate);
        }
        if(deptid != 0){
            queryWrapper.eq(Register::getDeptid,deptid);
        }
        if(! "null".equals(realname)){
            queryWrapper.like(Register::getRealname,realname);
        }
        return registerService.list(queryWrapper);
    }
//
//    @RequestMapping("/conditionselect2")
//    public List<Register> conditionselect2(@RequestParam Integer deptid,
//                                          @RequestParam Integer visitstate){
//        LambdaQueryWrapper<Register> queryWrapper = new LambdaQueryWrapper<>( );
//        if(deptid != null){
//            queryWrapper.eq(Register::getDeptid,deptid);
//        }
//        if(visitstate != null){
//            queryWrapper.eq(Register::getVisitstate,visitstate);
//        }
//        return registerService.list(queryWrapper);
//    }
//    @RequestMapping("/conditionselect")
//    public List<Register> conditionselect(@RequestBody Register register){
//        LambdaQueryWrapper<Register> queryWrapper = new LambdaQueryWrapper<>( );
//        if(register.getUserid() != null){
//            queryWrapper.eq(Register::getUserid,register.getUserid());
//        }
//        if(register.getVisitstate() != null){
//            queryWrapper.eq(Register::getVisitstate,register.getVisitstate());
//        }
//        if(register.getDeptid() != null){
//            queryWrapper.eq(Register::getDeptid,register.getDeptid());
//        }
//        return registerService.list(queryWrapper);
//    }
    @RequestMapping("/updatestate")
    public Boolean updatestate(@RequestParam Integer registid,
                               @RequestParam Integer visitstate){
        Register register = new Register();
        register.setId(registid);
        register.setVisitstate(visitstate);
        return registerService.updateById(register);
    }
}
